import request from '@/utils/request';
import { parseStrEmpty } from '@/utils/honghu';

/**
 * 查询用户列表
 * @param query
 */
export const listUser = (query) => {
	return request({
		url: '/system/user/list',
		method: 'get',
		params: query
	});
};

/**
 * 通过用户ids查询用户
 * @param userIds
 */
export const optionSelect = (userIds) => {
	return request({
		url: '/system/user/optionselect?userIds=' + userIds,
		method: 'get'
	});
};

/**
 * 获取用户详情
 * @param userId
 */
export const getUser = (userId) => {
	return request({
		url: '/system/user/' + parseStrEmpty(userId),
		method: 'get'
	});
};

/**
 * 新增用户
 */
export const addUser = (data) => {
	return request({
		url: '/system/user',
		method: 'post',
		data: data
	});
};

/**
 * 修改用户
 */
export const updateUser = (data) => {
	return request({
		url: '/system/user',
		method: 'put',
		data: data
	});
};

/**
 * 删除用户
 * @param userId 用户ID
 */
export const delUser = (userId) => {
	return request({
		url: '/system/user/' + userId,
		method: 'delete'
	});
};

/**
 * 用户密码重置
 * @param userId 用户ID
 * @param password 密码
 */
export const resetUserPwd = (userId, password) => {
	const data = {
		userId,
		password
	};
	return request({
		url: '/system/user/resetPwd',
		method: 'put',
		headers: {
			isEncrypt: true,
			repeatSubmit: false
		},
		data: data
	});
};

/**
 * 用户状态修改
 * @param userId 用户ID
 * @param status 用户状态
 */
export const changeUserStatus = (userId, status) => {
	const data = {
		userId,
		status
	};
	return request({
		url: '/system/user/changeStatus',
		method: 'put',
		data: data
	});
};

/**
 * 查询用户个人信息
 */
export const getUserProfile = () => {
	return request({
		url: '/system/user/profile',
		method: 'get'
	});
};

/**
 * 修改用户个人信息
 * @param data 用户信息
 */
export const updateUserProfile = (data) => {
	return request({
		url: '/system/user/profile',
		method: 'put',
		data: data
	});
};

/**
 * 用户密码重置
 * @param oldPassword 旧密码
 * @param newPassword 新密码
 */
export const updateUserPwd = (oldPassword, newPassword) => {
	const data = {
		oldPassword,
		newPassword
	};
	return request({
		url: '/system/user/profile/updatePwd',
		method: 'put',
		headers: {
			isEncrypt: true,
			repeatSubmit: false
		},
		data: data
	});
};

/**
 * 用户头像上传
 * @param data 头像文件
 */
export const uploadAvatar = (data) => {
	return request({
		url: '/system/user/profile/avatar',
		method: 'post',
		data: data
	});
};

/**
 * 查询授权角色
 * @param userId 用户ID
 */
export const getAuthRole = (userId) => {
	return request({
		url: '/system/user/authRole/' + userId,
		method: 'get'
	});
};

/**
 * 保存授权角色
 * @param data 用户ID
 */
export const updateAuthRole = (data) => {
	return request({
		url: '/system/user/authRole',
		method: 'put',
		params: data
	});
};

/**
 * 查询当前部门的所有用户信息
 * @param deptId
 */
export const listUserByDeptId = (deptId) => {
	return request({
		url: '/system/user/list/dept/' + deptId,
		method: 'get'
	});
};

/**
 * 查询部门下拉树结构
 */
export const deptTreeSelect = () => {
	return request({
		url: '/system/user/deptTree',
		method: 'get'
	});
};

export default {
	listUser,
	getUser,
	optionSelect,
	addUser,
	updateUser,
	delUser,
	resetUserPwd,
	changeUserStatus,
	getUserProfile,
	updateUserProfile,
	updateUserPwd,
	uploadAvatar,
	getAuthRole,
	updateAuthRole,
	deptTreeSelect,
	listUserByDeptId
};
